<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wuwu <15093565100@163.com>
// +----------------------------------------------------------------------
namespace api\portal\controller;

use api\portal\model\PortalPostModel;
use cmf\controller\RestBaseController;
use think\Db;

class UserController extends RestBaseController
{
    protected $postModel;

    public function __construct(PortalPostModel $postModel)
    {
        parent::__construct();
        $this->postModel = $postModel;
    }

    /**
     * 会员文章列表
     */
    public function articles()
    {
        $userId = $this->request->param('user_id', 0, 'intval');

        if (empty($userId)) {
            $this->error('用户id不能空！');
        }

        $data = $this->request->param();
        $articles = $this->postModel->setCondition($data)->where(['user_id' => $userId])->select();

        if (count($articles) == 0) {
            $this->error('没有数据');
        } else {
            $this->success('ok', ['list' => $articles]);
        }

    }

    public function getUserInfo()
    {
        $user_id = $this->request->param('user_id', 0, 'intval');
        return Db::table("zp_user")->find($user_id);
    }

    public function updateUserInfo()
    {
        $data = $this->request->param();
        $id = $data['id'];
        unset($data['id']);
        return Db::table("zp_user")->where(['id' => $id])->update($data);
    }

    public function addWorkExperence()
    {
        $data = $this->request->param();
        $data['create_time'] = time();
        if (isset($data['start_time'])) {
            $data['start_time'] = substr_replace($data['start_time'], "-", ".");
        }
        if (isset($data['end_time'])) {
            $data['end_time'] = substr_replace($data['end_time'], "-", ".");
        }
        return Db::table("zp_workexperience")->insert($data);
    }

    public function updateWorkExperence()
    {
        $data = $this->request->param();
        $id = $data['id'];
        unset($data['id']);
        if (isset($data['start_time'])) {
            $data['start_time'] = substr_replace($data['start_time'], "-", ".");
        }
        if (isset($data['end_time'])) {
            $data['end_time'] = substr_replace($data['end_time'], "-", ".");
        }
        return Db::table("zp_workexperience")->where(['id' => $id])->update($data);
    }

    public function getWorkExperenceList()
    {
        $data = $this->request->param();
        return Db::table("zp_workexperience")->where($data)->order("create_time desc")->select();
    }

    public function addEducation()
    {
        $data = $this->request->param();
        $data['create_time'] = time();
        if (isset($data['start_time'])) {
            $data['start_time'] = substr_replace($data['start_time'], "-", ".");
        }
        if (isset($data['end_time'])) {
            $data['end_time'] = substr_replace($data['end_time'], "-", ".");
        }
        return Db::table("zp_education")->insert($data);
    }

    public function updateEducation()
    {
        $data = $this->request->param();
        $id = $data['id'];
        unset($data['id']);
        if (isset($data['start_time'])) {
            $data['start_time'] = substr_replace($data['start_time'], "-", ".");
        }
        if (isset($data['end_time'])) {
            $data['end_time'] = substr_replace($data['end_time'], "-", ".");
        }
        return Db::table("zp_education")->where(['id' => $id])->update($data);
    }

    public function getEducationList()
    {
        $data = $this->request->param();
        return Db::table("zp_education")->where($data)->order("create_time desc")->select();
    }

    public function getMyCollections()
    {
        $data = $this->request->param();
        $userid = $data['userid'];
        return Db::table("zp_favor")
            ->join("zp_jobs", "job_id=zp_jobs.id")
            ->where(['zp_favor.user_id' => $userid])
            ->field("zp_jobs.name,FROM_UNIXTIME(zp_favor.create_time,'%Y.%m.%d') create_time,zp_jobs.id id")
            ->order("zp_favor.create_time desc")
            ->select();
            // echo Db::table("zp_favor")->getLastSql();die;
    }

    public function getMyDelivery()
    {
        $data = $this->request->param();
        $userid = $data['userid'];
        return Db::table("zp_delivery")
            ->join("zp_jobs", "job_id=zp_jobs.id")
            ->where(['zp_delivery.user_id' => $userid])
            ->field("zp_jobs.name,FROM_UNIXTIME(zp_delivery.create_time,'%Y.%m.%d') create_time,zp_jobs.id")
            ->order("zp_delivery.create_time desc")
            ->select();
            // echo Db::table("zp_delivery")->getLastSql();die;
    }

}
